/**
 * 
 */
package sailing.ui.systemsetting;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.BankName;
import sailing.entity.Customer;
import sailing.listener.UI;
import sailing.managers.BankNameManager;
import sailing.managers.CustomerManager;
import sailing.ui.base.CommonJDialog;
import cjlite.utils.RandomUtils;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class NewCustomerDialog extends CommonJDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField nameTF;
	private JTextField codeTF;
	private JTextField addressTF, zipcodeTF, faxTF, telTf, contactTF, vatNumTF, bankNumberTF;
	private JComboBox bankCombo;
	private Customer editCustomer;

	private static String[] btnCaptions = { "确定", "取消" };


	/**
	 * @param singleFrameApplication
	 * @param owner
	 * @param modal
	 */
	public NewCustomerDialog(Window window, SingleFrameApplication singleFrameApplication) {
		super(window, "添加新的客户", ModalityType.APPLICATION_MODAL, singleFrameApplication);
	}


	@Override
	protected JPanel initialContentPanel() {
		// JPanel root = new FormDebugPanel(true, true);
		JPanel root = new JPanel();
		root.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		FormLayout layout = new FormLayout("right:pref,4dlu,50dlu,4dlu,right:50dlu,4dlu,80dlu",
				"22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu");
		//

		root.setLayout(layout);
		PanelBuilder pb = new PanelBuilder(layout);

		CellConstraints cc = new CellConstraints();
		// row 1
		root.add(new JLabel("客户代码："), cc.xy(1, 1));
		codeTF = new JTextField();
		root.add(codeTF, cc.xywh(3, 1, 3, 1));
		// row 3
		root.add(new JLabel("客户名称："), cc.xy(1, 3));
		nameTF = new JTextField();
		root.add(nameTF, cc.xywh(3, 3, 5, 1));
		// row 5
		root.add(new JLabel("地址："), cc.xy(1, 5));
		addressTF = new JTextField();
		root.add(addressTF, cc.xywh(3, 5, 5, 1));
		// row 7
		root.add(new JLabel("邮编："), cc.xy(1, 7));
		zipcodeTF = new JTextField();
		root.add(zipcodeTF, cc.xy(3, 7));
		root.add(new JLabel("传真："), cc.xy(5, 7));
		faxTF = new JTextField();
		root.add(faxTF, cc.xy(7, 7));

		// row 9
		root.add(new JLabel("电话："), cc.xy(1, 9));
		telTf = new JTextField();
		root.add(telTf, cc.xywh(3, 9, 3, 1));

		// row 11
		root.add(new JLabel("联系人："), cc.xy(1, 11));
		contactTF = new JTextField();
		root.add(contactTF, cc.xywh(3, 11, 3, 1));

		// row 13
		root.add(new JLabel("增值税税务登记号："), cc.xy(1, 13));
		contactTF = new JTextField();
		root.add(contactTF, cc.xywh(3, 13, 5, 1));

		// row 15
		root.add(new JLabel("增值税开户银行及账户："), cc.xy(1, 15));
		Box bank = Box.createHorizontalBox();
		bankCombo = new JComboBox();
		bank.add(bankCombo);
		bank.add(Box.createHorizontalStrut(5));
		bankNumberTF = new JTextField();
		bank.add(bankNumberTF);
		root.add(bank, cc.xywh(3, 15, 5, 1));

		this.setSize(620, 530);
		this.updateData();
		return root;
	}


	private void updateData() {
		BankNameManager bnm = this.getApplication().getContext().getIoc().getInstance(BankNameManager.class);
		
		bankCombo.addItem("请选择银行名称");
		for(BankName bn:bnm.getAllBankNameList()){
			bankCombo.addItem(bn.getName());
		}
	}

	

	@Override
	protected void performBtnClick(String btnCaption) {
		if (btnCaption.equalsIgnoreCase(btnCaptions[0])) {
			this.performOKAction();
		} else {
			this.performCancelAction();
		}
	}


	@Override
	protected String[] getBottomBtnTxt() {
		return btnCaptions;
	}


	protected void initialUI_bak() {
		JPanel root = new JPanel();

		root.setLayout(new BorderLayout(5, 5));

		Box all = Box.createVerticalBox();

		all.add(Box.createVerticalStrut(10));

		Box box1 = Box.createHorizontalBox();
		box1.add(new JLabel("新客户名称："));
		box1.add(Box.createHorizontalStrut(10));
		nameTF = new JTextField() {

			@Override
			public Dimension getPreferredSize() {
				return getMinimumSize();
			}


			@Override
			public Dimension getMinimumSize() {
				return new Dimension(200, 30);
			}
		};
		// nameTF.setMinimumSize(new Dimension(30,200));

		if (editCustomer != null) {
			nameTF.setText(this.editCustomer.getName());
		}

		box1.add(nameTF);

		all.add(box1);

		all.add(Box.createVerticalStrut(10));

		Box box2 = Box.createHorizontalBox();
		box2.add(Box.createHorizontalStrut(100));
		JButton okBtn = new JButton("确定");
		okBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				performOKAction();
			}
		});
		box2.add(okBtn);
		box2.add(Box.createHorizontalStrut(10));
		JButton cancelBtn = new JButton("取消");
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				performCancelAction();
			}
		});
		box2.add(cancelBtn);

		all.add(box2);
		all.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		root.add(all, BorderLayout.CENTER);

		this.setLayout(new BorderLayout(5, 5));
		this.add(root, BorderLayout.CENTER);
		this.setSize(380, 150);
	}


	protected void performCancelAction() {
		this.setVisible(false);
	}


	protected void performOKAction() {
		String cname = nameTF.getText();
		if (cname.trim().length() == 0) {
			JOptionPane.showMessageDialog(this, "请输入客户名称。", "提示！", JOptionPane.WARNING_MESSAGE);
			return;
		}
		cname = cname.trim();
		CustomerManager cm = this.getApplication().getContext().getIoc().getInstance(CustomerManager.class);

		Customer c;
		if (this.editCustomer == null) {
			c = new Customer();
			c.setId(RandomUtils.randomString16());
			c.setName(cname);
			c.setCreateTime(System.currentTimeMillis());
			String msg = cm.save(c);
			if (msg == null || msg.length() == 0) {
				JOptionPane.showMessageDialog(this, "添加成功！");
				this.setVisible(false);
				UI.refresh();
			} else {
				JOptionPane.showMessageDialog(this, msg, "提示！", JOptionPane.WARNING_MESSAGE);
			}

		} else {
			this.editCustomer.setName(cname);
			String msg = cm.update(this.editCustomer);
			if (msg == null || msg.length() == 0) {
				JOptionPane.showMessageDialog(this, "编辑成功！");
				this.setVisible(false);
				UI.refresh();
			} else {
				JOptionPane.showMessageDialog(this, msg, "提示！", JOptionPane.WARNING_MESSAGE);
			}
		}

	}


	public void setEditCustomer(Customer cc) {
		this.editCustomer = cc;
		if (this.nameTF != null) {
			this.nameTF.setText(cc.getName());
		}
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Dialog#isResizable()
	 */
	@Override
	public boolean isResizable() {
		return false;
	}

}